package com.cwrl.spider.server.test.conf;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.Log;
import com.cwrl.spider.common.mq.core.SpiderMqTemplate;
import com.cwrl.spider.server.test.flow.price.PriceContext;
import com.cwrl.spider.server.test.listener.message.TestMessage;
import com.yomahub.liteflow.builder.el.ELBus;
import com.yomahub.liteflow.builder.el.ELWrapper;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.builder.el.ThenELWrapper;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.proxy.LiteFlowMethodBean;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SetupCommand implements CommandLineRunner {

    private final SpiderMqTemplate spiderMqTemplate;

    private final FlowExecutor flowExecutor;

    @Override
    public void run(String... args) throws Exception {

        ThreadUtil.execute(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(10000L);
                    TestMessage message = new TestMessage();
                    message.setName("测试数据 " + i ++);
                    spiderMqTemplate.send("my-test", message);
                    spiderMqTemplate.sendByBroadcast("my-test1", message);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        ELWrapper el = ELBus.then("priceInitNode", "ticketNode","discountNode");
        LiteFlowChainELBuilder.createChain().setChainId("priceChain").setEL(
                el.toEL()
        ).build();

        LiteflowResponse priceChain = flowExecutor.execute2Resp("priceChain", "548", PriceContext.class);
        PriceContext contextBean = priceChain.getContextBean(PriceContext.class);
        System.out.println(contextBean.getTotalPrice());


    }
}
